package moriarty.gwt.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class SubmissionHandler implements ClickHandler, KeyUpHandler{

	private SubmissionTab submissionTab;
	private SubmissionDialog submissionDialog;
	private SubmissionWidget submissionWidget;
	private final SotdServiceAsync sotdService = GWT.create(SotdService.class);
	
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";
	
	public SubmissionHandler(SubmissionTab submissionTab, 
							 SubmissionDialog submissionDialog) {	
		this.submissionTab = submissionTab;
		this.submissionDialog = submissionDialog;
		this.submissionWidget = submissionTab.getSubmissionWidget();
	}

	/**
	 * Fired when the user clicks on the sendButton.
	 */
	public void onClick(ClickEvent event) {
		sendSotdToServer();
	}

	/**
	 * Send the name from the nameField to the server and wait for a response.
	 */
	private void sendSotdToServer() {
		
		// Send the submission to the server for processing, and storage
		sotdService.storeSubmission(submissionWidget.makeSubmission(), new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Should be logging this stuff	
			}
			@Override
			public void onSuccess(Void result) {
				// TODO Should be logging this stuff	
				PreviousSubmissionTab.getInstance().update();
			}				
		});
		
		// First, we convert everything to fancy HTML for the popup message to display
		submissionDialog.getTextToServerHTML().setHTML(submissionWidget.getCompleteHTML());

		// Disable the send button to prevent duplicate sends
		submissionTab.getSendButton().setEnabled(false);

		//Clear the server response label
		submissionDialog.getServerResponseLabel().setText("");
		
		//Scrape the email list box and update our Datastore with it
		sotdService.updateEmails(submissionWidget.getSubscribers(), new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Should be logging this stuff	
			}
			@Override
			public void onSuccess(Void result) {
				// TODO Should be logging this stuff	
			}				
		});
		
		//Now we send the submission
		sotdService.sotdServer(submissionWidget.getCompleteMessage(),
				new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						submissionDialog.getDialogBox().setText("SOTD Bot Server - Failure");
						submissionDialog.getServerResponseLabel().addStyleName("serverResponseLabelError");
						submissionDialog.getServerResponseLabel().setHTML(SERVER_ERROR);
						submissionDialog.getDialogBox().center();
						submissionDialog.getCloseButton().setFocus(true);
					}

					public void onSuccess(String result) {
						submissionDialog.getDialogBox().setText("SOTD Bot Server");
						submissionDialog.getServerResponseLabel().removeStyleName("serverResponseLabelError");
						if(!result.isEmpty()){
							submissionDialog.getServerResponseLabel().setHTML("Unable to send to: " + result);
						}
						else{
							submissionDialog.getServerResponseLabel().setHTML("Everyone recieved the message!");
						}
						
						submissionDialog.getDialogBox().center();
						submissionDialog.getCloseButton().setFocus(true);
					}
				});
	}

	@Override
	public void onKeyUp(KeyUpEvent event) {
		onClick(null);
	}
	
	
}
